from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class AddressManagementPage:
    def __init__(self, driver):
        self.driver = driver
        # 页面元素定位器
        self.add_address_button_locator = (By.ID, "add-address-button")
        self.edit_address_button_locator = (By.ID, "edit-address-button")
        self.delete_address_button_locator = (By.ID, "delete-address-button")
        self.address_list_locator = (By.ID, "address-list")
        self.address_details_locator = (By.ID, "address-details")
        self.address_name_locator = (By.ID, "address-name")
        self.address_street_locator = (By.ID, "address-street")
        self.address_city_locator = (By.ID, "address-city")
        self.address_state_locator = (By.ID, "address-state")
        self.address_zipcode_locator = (By.ID, "address-zipcode")
        self.address_phone_locator = (By.ID, "address-phone")
        self.save_changes_button_locator = (By.ID, "save-changes-button")
        self.cancel_changes_button_locator = (By.ID, "cancel-changes-button")
        self.empty_list_message_locator = (By.ID, "empty-list-message")

    def navigate(self):
        self.driver.get('http://example.com/addresses')

    def add_new_address(self):
        add_address_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.add_address_button_locator)
        )
        add_address_button.click()

    def edit_address(self):
        edit_address_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.edit_address_button_locator)
        )
        edit_address_button.click()

    def delete_address(self):
        delete_address_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.delete_address_button_locator)
        )
        delete_address_button.click()

    def get_address_list(self):
        address_list = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.address_list_locator)
        )
        return address_list

    def get_address_details(self):
        address_details = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.address_details_locator)
        )
        return address_details.text

    def set_address_name(self, name):
        address_name = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.address_name_locator)
        )
        address_name.clear()
        address_name.send_keys(name)

    def set_address_street(self, street):
        address_street = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.address_street_locator)
        )
        address_street.clear()
        address_street.send_keys(street)

    def set_address_city(self, city):
        address_city = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.address_city_locator)
        )
        address_city.clear()
        address_city.send_keys(city)

    def set_address_state(self, state):
        address_state = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.address_state_locator)
        )
        address_state.clear()
        address_state.send_keys(state)

    def set_address_zipcode(self, zipcode):
        address_zipcode = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.address_zipcode_locator)
        )
        address_zipcode.clear()
        address_zipcode.send_keys(zipcode)

    def set_address_phone(self, phone):
        address_phone = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.address_phone_locator)
        )
        address_phone.clear()
        address_phone.send_keys(phone)

    def save_changes(self):
        save_changes_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.save_changes_button_locator)
        )
        save_changes_button.click()

    def cancel_changes(self):
        cancel_changes_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.cancel_changes_button_locator)
        )
        cancel_changes_button.click()

    def get_empty_list_message(self):
        empty_list_message = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.empty_list_message_locator)
        )
        return empty_list_message.text